<template>
	<view>
		<view style="height: 250rpx; margin: 40rpx ; margin-top: 140rpx;" class="grid-item-box">
			<image mode="widthFix" style="width: 40%; align-items: center;" src="../../static/icon/技街图标.png" ></image>
			
		</view>
		<view style="margin: 50rpx ;">
			<view style="font-size: 44rpx;text-align: center; font-weight: bold;">欢迎来到技大堕落街</view>
			<!-- <uni-easyinput v-model="username" placeholder="请输入用户名" prefix-icon="person"></uni-easyinput>
			<uni-easyinput type="password" v-model="password" placeholder="请输入密码" prefix-icon="person"></uni-easyinput> -->
			<button type="primary" style="background-color:#fdcd1c; margin: 60rpx 10rpx; border-radius: 60rpx;" @click="getUserInfo()">微信一键登录</button> 
		</view>
	</view>
</template>

<script>
	import { baseUrl } from '@/config/config.js';
	export default {
		data() {
			return {
				password: '' ,
				username: '' ,
				token: ''
			}
		},
		methods: {
			toIndexPage(){
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			getUserInfo(){
				wx.getUserProfile({
					desc: '获取用户的头像,用户名',
					success: (res) => {
						console.log(res.userInfo)
						uni.setStorageSync('username',res.userInfo.nickName)
						uni.setStorageSync('avatar',res.userInfo.avatarUrl)
					}
				})
				//获取微信用户的授权码
				wx.login({
					success: (res)=>{
						//console.log(res.code)
						uni.request({
							url: baseUrl+'/user/login',
							method: 'POST',
							data: {
								code: res.code
							},
							success: (loginRes) => {
								let token= loginRes.data.data?.token
								let openid=loginRes.data.data?.openid	
								if(token!=null)uni.setStorageSync('token',token)
								if(openid!=null)uni.setStorageSync('openid',openid)
								console.log(loginRes.data.data)
								console.log('token:'+uni.getStorageSync('token'))
								console.log('openid:'+openid)
								this.toIndexPage()
							},
							fail: () => {
								uni.showToast({
									title: '服务器连接失败',
									icon: 'error'
								})
							}
						})
					}
				})
				
			}
		}
	}
</script>

<style>
	.grid-item-box {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
		}
</style>
